﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

using LicenseManager;
using Utilities;

using Microsoft.Win32;

namespace UILibrary
{
    public partial class Registration : Form
    {
        //private Utilities.DbManager m_DBManager = null;
        private DbOperations m_dbOperations;
        
        public Registration()
        {
            InitializeComponent();
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.FrameworkCultures))
            {
                RegionInfo ri = null;
                try
                {
                    ri = new RegionInfo(ci.Name);
                    CountryComboBox.Items.Add(ri.EnglishName);
                }
                catch
                {
                    // If a RegionInfo object could not be created we don't want to use the CultureInfo
                    //    for the country list.
                    continue;
                }

            }
        }

        public Registration(DbOperations dbOperations)
        {
           // m_DBManager = m_Db; 
            m_dbOperations = dbOperations;
            InitializeComponent();
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.FrameworkCultures))
            {
                RegionInfo ri = null;
                try
                {
                    ri = new RegionInfo(ci.Name);
                    CountryComboBox.Items.Add(ri.EnglishName);
                }
                catch
                {
                    // If a RegionInfo object could not be created we don't want to use the CultureInfo
                    //    for the country list.
                    continue;
                }

            }
        }

        private void activate_Click(object sender, EventArgs e)
        {
            if (ValidateInput() == true)
            {
                try
                {
                    RegisterUser();

                    RegistryKey kHKCU = Registry.CurrentUser;
                    RegistryKey kFirstRun = kHKCU.OpenSubKey("Software\\UpriseApps\\OfficeInCloudExcel2007", true);
                    if (kFirstRun != null)
                    {
                        kFirstRun.SetValue("FirstRun", 0);
                    }

                    this.DialogResult = DialogResult.OK;
                }
                catch(Exception ex)
                {
                    Utilities.TraceWrapper.LogException("activate_Click error: " + ex.Message, ex);

                    MessageBox.Show("We are sorry but your Office Integration unable to register new user. \n" +
                                 "Please contact The HOB.Biz support: info@thehob.biz for details.");

                    this.DialogResult = DialogResult.Abort;
                }
                finally
                {
                    this.Close();
                }
            }
        }

        private bool ValidateInput()
        {
            if (FirstNameBox.Text == "")
            {
                MessageBox.Show("Please fill in First Name");
                return false;
            }
            if (LastNameBox.Text == "")
            {
                MessageBox.Show("Please fill in Last Name");
                return false;
            }
            if (EmailTextBox.Text == "")
            {
                MessageBox.Show("Please fill in Email");
                return false;
            }
            if (PhoneTextBox.Text == "")
            {
                MessageBox.Show("Please fill in Phone");
                return false;
            }
            if (CountryComboBox.SelectedItem == null)
            {
                MessageBox.Show("Please select the County");
                return false;
            }
            return true; 
        }

        private void RegisterUser()
        {
            ZohoLicenseManager lm = new ZohoLicenseManager("andrew.kandzuba@upriseapps.com", "mamamilaramu", m_dbOperations);
            ZohoTypes.ZohoContact contact = null;

            lm.Login();

            contact = lm.SearchForContact(EmailTextBox.Text);
            if (contact == null)
            {
                contact = new ZohoTypes.ZohoContact();
                contact.LastName = LastNameBox.Text;
                contact.FirtsName = FirstNameBox.Text;
                contact.Email = EmailTextBox.Text;
                contact.Phone = PhoneTextBox.Text;
                //contact.LastLogin = DateTime.Now;
                contact.Company = CompanyTextBox.Text;
                contact.Country = (string)CountryComboBox.SelectedItem; 
                lm.InsertContact(contact);
            }
            else
            {
                //contact.LastLogin = DateTime.Now;
                lm.UpdateContact(contact); 
            }

            //lm.AddRegisterContact(contact, m_DBManager);
            m_dbOperations.AddRegisterContact(contact.FirtsName, contact.LastName, contact.Email);

            lm.Logout();
        }
    }
}
